import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import * as CodeGenerator from '@alilc/lowcode-code-generator/standalone-loader';
import { CodeGenActionBtn,workerJsUrl } from '../../components/CodeGenActionBtn';

var codeGeneratorPlugin = function codeGeneratorPlugin(ctx, options) {
  return {
    // 插件对外暴露的数据和方法
    exports: function exports() {
      return {
        generateCode: CodeGenerator.generateCode
      };
    },
    // 插件的初始化函数，在引擎初始化之后会立刻调用
    init: function init() {
      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.prev = 0;

                if (!(options !== null && options !== void 0 && options.disableCodeGenActionBtn)) {
                  ctx.skeleton.add({
                    type: 'Custom',
                    name: 'code-generator',
                    area: 'topArea',
                    props: {
                      align: 'right',
                      width: 100
                    },
                    content: CodeGenActionBtn,
                    contentProps: {
                      ctx: ctx
                    }
                  });
                } // 提前初始化下，这样后面用的时候更快


                CodeGenerator.init({workerJsUrl});
                _context.next = 9;
                break;

              case 5:
                _context.prev = 5;
                _context.t0 = _context["catch"](0);
                console.error('[plugin-code-geneator] failed to init: ', _context.t0);
                throw _context.t0;

              case 9:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, null, [[0, 5]]);
      }))();
    },
    destroy: function destroy() {}
  };
};

codeGeneratorPlugin.pluginName = 'codeGenerator';
codeGeneratorPlugin.meta = {
  dependencies: [],
  preferenceDeclaration: {
    title: 'codeGenerator 的参数定义',
    properties: [{
      key: 'disableCodeGenActionBtn',
      type: 'boolean',
      description: '是否要禁用出码的动作按钮'
    }]
  }
};
export default codeGeneratorPlugin;